﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Configuration;
using Projectmanagement.Entity;
using System.Web.Mvc;

namespace Projectmanagement.Utl
{
    public class Helpers
    {
        public static string DebugAccount
        {
            get
            {
                if (HttpContext.Current != null && HttpContext.Current.Items["DebugAccount"] == null)
                {
                    var settings = ConfigurationManager.AppSettings.GetValues("DebugAccount");

                    HttpContext.Current.Items["DebugAccount"] = settings[0] as String;
                }

                return (string)HttpContext.Current.Items["DebugAccount"];
            }
        }

        public static int GetFromDateTime(DateTime dt)
        {
            var firstDate = Convert.ToDateTime(dt.ToString("yyyy/MM/01")).DayOfWeek.ToString();
            switch (firstDate)
            {
                case "Monday":
                    return 1;
                case "Tuesday":
                    return 2;
                case "Wednesday":
                    return 3;
                case "Thursday":
                    return 4;
                case "Friday":
                    return 5;
                case "Saturday":
                    return 6;
                case "Sunday":
                    return 7;
            }
            return 0;
        }

        public static DateTime GetFirstDivDateTime(DateTime dt)
        {
            int firstint = GetFromDateTime(dt);
            var firstDate = Convert.ToDateTime(dt.ToString("yyyy/MM/01"));
            return firstDate.AddDays(-1 * (firstint % 7));
        }

        /// <summary>
        /// 获取用户参与的所有项目
        /// </summary>
        public static IQueryable<Project> GetUserProjects()
        {
            var lvl = Employee.CurrentRoleLVL;
            var user = Employee.CurrentUserName;

            if (lvl == RoleLevel.Manager)
            {
                return from p in CH.DB.Projects where p.Manager == user && p.IsActived select p;
            }
            else if (lvl < RoleLevel.Manager)
            {
                return from p in CH.DB.Projects where p.Members.Any(a => a.Name == user) && p.IsActived select p;
            }
            else
            {
                return from p in CH.DB.Projects where p.IsActived select p;
            }
        }

        public static IEnumerable<SelectListItem> TrafficTypeSelectList()
        {
            List<SelectListItem> selectList = new List<SelectListItem>();
            selectList.Add(new SelectListItem() { Text = "机场" });
            selectList.Add(new SelectListItem() { Text = "火车站" });
            selectList.Add(new SelectListItem() { Text = "地铁站" });
            return selectList;
        }
    }
}